import Vue from 'vue'

/**
 * 不过是vue-cli内置的
 * npm install --save vuex
 */
import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        vuexStoreCount: 0
    },
    getters: {
        vuexStoreCountFormat: (state) => {
            return "[[ " +  state.vuexStoreCount + " ]]";
        },
        vuexStoreCountFormatPlus: (state, getters) => {
            return "{{ " +  getters.vuexStoreCountFormat + " }}";
        }
    },
    mutations: {
        /**
         * 不要使用异步操作
         * @param state
         */
        increment(state) {
            state.vuexStoreCount ++;
        },
        /**
         * 不要使用异步操作
         * @param state
         */
        decrease(state) {
            state.vuexStoreCount --;
        },
        /**
         * 不要使用异步操作
         * @param state
         * @param params
         */
        incrementByParams(state, params) {
            state.vuexStoreCount += params.count;
        }
    },
    actions: {
        asyncIncrease(context) {
            /**
             * 异步新增
             */
            return new Promise(resolve => {
                setTimeout(() => {
                    context.commit('increment');
                    resolve();
                }, 1000)
            })
        }
    },
    modules: {}
})
